<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="inc/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <%@ include file="inc/meta.jsp" %>
    <meta name="description" content="">
    <meta name="author" content="">
    <!--引入wangEditor.css-->
    <link rel="stylesheet" type="text/css" href="static/wangEditor-2.1.22/dist/css/wangEditor.min.css">
    <!--引入jquery和wangEditor.js-->   <!--注意：javascript必须放在body最后，否则可能会出现问题-->
    <script type="text/javascript" src="static/wangEditor-2.1.22/dist/js/lib/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="static/wangEditor-2.1.22/dist/js/wangEditor.min.js"></script>
    <title>添加谈心</title>
    <%@ include file="inc/css.jsp" %>
</head>
<body>

<div id="posts" class="wrapper">

    <%@ include file="inc/nav.jsp" %>

    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">谈心列表</h1>
                <h4 style="margin-left: 10px;" id="showH">——添加谈心</h4>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <form id="infoForm" method="post" action="backend/mind/save" class="form-horizontal nice-validator n-default" role="form" novalidate="novalidate" enctype="multipart/form-data">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">标题:</label>

                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="title" name="title" maxlength="20" placeholder="请输入标题"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">打开金额/元:</label>

                                <div class="col-sm-2">
                                    <input type="number" class="form-control" id="openMoney" name="openMoney" maxlength="20" placeholder="请输入打开金额"/>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">封面:</label>

                                <div class="col-sm-4">
                                    <input type="file" name="cover" id="mainImage" style="display:none;"/>
                                    <a href="javascript:void(0);" onclick="_mindInfo.fn.AddImg()">
                                        <img id="mainPicture" src="static/images/add.jpg" style="height: 200px; width: 200px; display: inline; margin-bottom: 5px;" border="1"/>
                                    </a>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">内容:</label>

                                <div class="col-sm-8">
                                    <div id="div1" style="height: 400px;"></div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-6" style="text-align: center">
                                    <button type="button" class="btn btn-primary" onclick="_mindInfo.fn.subInfo()">确定添加</button>
                                    <button type="button" class="btn btn-primary" onclick="_mindInfo.fn.goBack()">返回</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <!-- /.panel-body -->

                </div>
                <!-- /.panel -->
            </div>
        </div>

    </div>
    <!-- /#page-wrapper -->

</div>
<!-- /#wrapper -->

<%@ include file="inc/footer.jsp" %>

</body>

<script type="text/javascript">
    var _mindInfo = {
        v: {
            editor: null
        },
        fn: {
            init: function () {
                // 阻止输出log
                wangEditor.config.printLog = false;

                _mindInfo.v.editor = new wangEditor('div1');

                // 关闭js过滤
                _mindInfo.v.editor.config.jsFilter = false;

                // 普通的自定义菜单
                _mindInfo.v.editor.config.menus = [
                    'bold',
                    'underline',
                    'italic',
                    'strikethrough',
                    'eraser',
                    'forecolor',
                    'bgcolor',
                    '|',
                    'quote',
                    'fontfamily',
                    'fontsize',
                    'head',
                    'unorderlist',
                    'orderlist',
                    'alignleft',
                    'aligncenter',
                    'alignright',
                    '|',
                    'link',
                    'unlink',
                    'img',
                    '|',
                    'fullscreen'
                ];

                // 上传图片（举例）
                _mindInfo.v.editor.config.uploadImgUrl = '/52hz/common/addTempImageEditor';

                // 自定义上传图片的filename
                _mindInfo.v.editor.config.uploadImgFileName = 'customFileName';

                // 隐藏掉插入网络图片功能。该配置，只有在你正确配置了图片上传功能之后才可用。
                _mindInfo.v.editor.config.hideLinkImg = true;

                _mindInfo.v.editor.create();

                $("#mainImage").uploadPreview({
                    Img: "mainPicture",
                    Width: 200,
                    Height: 170
                });
            },
            AddImg: function () {
                // a标签绑定onclick事件
                $('#mainImage').click();
            },
            checkData: function () {
                var flag = true;
                var title = $('#title').val();
                var money = $('#openMoney').val();
                var cover = $('#mainPicture').prop('src');
                var content = _mindInfo.v.editor.$txt.html();

                if (null == title || title == '') {
                    $leoman.notify('标题不能为空', "error");
                    flag = false;
                    return;
                }

                if (null == money || money == '') {
                    $leoman.notify('打开金额不能为空', "error");
                    flag = false;
                    return;
                }

                if (Number(money) <= Number(0)) {
                    $leoman.notify("打开金额需大于0", "error");
                    flag = false;
                    return;
                }

                if (null == cover || cover == '' || cover == _basePath + 'static/images/add.jpg' || cover == 'static/images/add.jpg') {
                    $leoman.notify("请上传封面图片", "error");
                    flag = false;
                    return;
                }

                if (null == content || content == '' || content == '<p><br></p>') {
                    $leoman.notify('内容不能为空', "error");
                    flag = false;
                    return;
                }

                return flag;
            },
            subInfo: function () {
                // 所有的验证通过后，执行新增操作
                if (_mindInfo.fn.checkData()) {
                    $("#infoReduceDialogModal").modal("show");

                    $("#infoForm").ajaxSubmit({
                        url: _basePath + "backend/mind/save",
                        dataType: "json",
                        data: {
                            "txtVal": _mindInfo.v.editor.$txt.text(),
                            "content": _mindInfo.v.editor.$txt.html()
                        },
                        success: function (data) {
                            $('#infoReduceDialogModal').modal("hide");
                            if (data == 1) {
                                _mindInfo.fn.goBack();
                            } else {
                                $leoman.notify("操作失败", "error");
                            }
                        }
                    });
                }
            },
            goBack: function () {
                window.location.href = "backend/mind/index";
            }
        }
    }

    $(document).ready(function () {
        _mindInfo.fn.init();
    });

</script>

</html>